package me.nibo.bignerdranch.hellomoon;

import android.content.Context;
import android.media.MediaPlayer;
import android.util.Log;

/**
 * @author nibo
 */
public class AudioPlayer {
    private static final String TAG = AudioPlayer.class.getSimpleName();
    private MediaPlayer mPlayer;

    /**
     * 停止播放
     */
    public void stop() {
        if (mPlayer != null) {
            mPlayer.release();
            mPlayer = null;
        }
    }

    /**
     * 开始播放
     * @param context 应用上下文
     */
    public void start(Context context) {
        if (mPlayer == null) {
            mPlayer = MediaPlayer.create(context, R.raw.one_small_step);
            mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    Log.d(TAG, "播放完成！");
                    stop();
                }
            });
        }
        mPlayer.start();
    }

    /**
     * 暂停播放
     */
    public void pause() {
        if (mPlayer != null) {
            mPlayer.pause();
        }
    }

}
